#include <avr/pgmspace.h>
#include <Arduino.h>
#include <Wire.h>
#include <MemoryFree.h>
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Keypad.h>
#include <Streaming.h>
#include <EEPROM.h>
#include <DS1307RTC.h>
#include <Time.h>

#include "utility.h";
#include "Aquarium_Devices.h";
#include "Dosing_Pumps.h";
#include "Clock.h";
#include "Temp.h";
#include "Gui.h";
#include "My_Keypad.h";
#include "Aquarium_Modes.h";
#include "Water_Level.h";
#include "Alerts.h";



LiquidCrystal lcd(36,37,38,39,40,41);


Aquarium_Devices aquarium_devices = Aquarium_Devices();
Dosing_Pumps            dosing_pumps;
Aquarium_Modes       aquarium_modes;
Clock                             clock;
Temp                              temp;
Gui                               gui;
Water_Level                       water_level;
Alerts                            alerts;

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

void Init_Wait_Time_Func(){

 byte Init_Start_Time = EEPROM.read(EEPROM_GEN_INIT_START_TIME);
  if(Init_Start_Time != 0 ){
    for(byte i = 0 ; i < Init_Start_Time ; ++i ){
      lcd.setCursor(0, 1);
      strcpy_P(buffer, (char*)pgm_read_word(&(  gen_string_table[  WAITING_STRING ]     )));
      lcd.print(buffer );
      lcd.print(Init_Start_Time-i);
      strcpy_P(buffer, (char*)pgm_read_word(&(  gen_string_table[  MINUTES_STRING ]     )));
      lcd.print(buffer );
      Wait_1Min();
      lcd.clear();
    }
  }
  
}

void setup()			  
{
  lcd.begin(LCD_COLUMNS, LCD_LINES);
  //Serial.begin(9600);
  lcd.setCursor(0, 1);
  strcpy_P(buffer, (char*)pgm_read_word(&(  gen_string_table[  INITIALIZE_CLOCK_STRING ]     )));
  lcd.print(buffer );
  clock.init();
  aquarium_devices.update_devices();
  delay(1000);
  lcd.clear();
  lcd.setCursor(0, 1);
  strcpy_P(buffer, (char*)pgm_read_word(&(  gen_string_table[  INITIALIZE_TEMP_STRING ]     )));
  lcd.print(buffer );
  temp.init();
 
  delay(1000);
  lcd.clear();

  water_level.init();
 

  
  
 //lcd.print("END EEPROM = ");
 //lcd.print(EEPROM_DEVICE_ADDRESS_START+EEPROM_DEVICE_ADDRESS_OFFSET*NUMBER_OF_DEVICES);
 // delay(5000);
 // lcd.clear();
 
}


void loop()			  
{
  float tempC;
  
  clock.update_clock();

  dosing_pumps.update_Pumps(new_min,new_sec);
  water_level.update( );
  alerts.Check_Alert();
  
  if( new_sec){
    temp.update_temp();
    gui.Update();
    aquarium_modes.update_sec();
  }

  if ( new_min ){
    aquarium_modes.update();
    dosing_pumps.Check_Pumps();
    aquarium_devices.update_devices();
  }


  {
    char key = keypad.getKey();
    if (key != NO_KEY){
      if(key == 'D'){
        lcd.begin(LCD_COLUMNS, LCD_LINES);
      }
      gui.Key_Press(key  ,false);
    }
  }
 
  
   /*
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());
   */
  /* 
   
   if (Serial.available() > 0) {
   char key = Serial.read() ;
   gui.Key_Press(key  ,false);
   }
   */
   
   
}













